﻿
using System;
using System.Collections.Generic;
using AgileBoard.Domain;

namespace AgileBoard.Dtos.Mappers
{
    public class TaskDTOMapper : BaseDTOMapper
    {
        public static TaskDTO MapToDTO(Task model)
        {
            var dto = new TaskDTO();
            IList<ActualsDTO> actualDTO = new List<ActualsDTO>();

            dto.ID = model.TaskID;
            dto.StoryID = model.Story == null ? "" : model.Story.StoryID.ToString();
            dto.Story = model.Story == null ? "" : model.Story.Name;
            dto.StoryColor = model.Story == null ? "" : model.Story.Color;
            dto.IterationID = model.IterationID;
            dto.Name = model.Name;
            dto.Color = model.Color;
            dto.Remaining = model.Remaining;
            dto.Evaluation = model.Evaluation;
            dto.Beyond = model.Beyond;
            dto.Card = model.Card;
            dto.Observations = model.Observations;
            dto.Primary = model.Primary;
            dto.StatusID = model.Status == null ? "" : model.Status.StatusID.ToString();
            dto.Status = model.Status == null ? "" : model.Status.Name;
            dto.CreateDate = model.CreateDate;
            dto.StartDate = model.StartDate;
            dto.FinishDate = model.FinishDate;
            dto.TargetDate = model.Target;
            dto.ConsigneeID = model.AssignedTo == null ? "" : model.AssignedTo.UserID.ToString();
            dto.Consignee = model.AssignedTo == null ? "" : model.AssignedTo.UserName;
            dto.CreateTime = model.CreateTime;
            dto.Creator = model.Creator;

            //load tasks
            foreach (Actual actual in model.Actuals)
            {
                actualDTO.Add(ActualsDTOMapper.MapToDTO(actual));
            }
            dto.Actuals = actualDTO;


            return dto;
        }

        public static Task MapFromDTO(TaskDTO dto)
        {
            var task = new Task();
            task.TaskID = dto.ID;
            task.Story = (dto.StoryID == null || dto.StoryID == "")
                             ? null
                             : ManagerFactory.StoryManager.Get(new Guid(dto.StoryID));
            task.IterationID = dto.IterationID;
            task.Name = dto.Name;
            task.Color = dto.Color;
            task.Remaining = dto.Remaining;
            task.Evaluation = dto.Evaluation;
            task.Beyond = dto.Beyond;
            task.CreateTime = DateTime.Now;
            task.Card = dto.Card;
            task.Observations = dto.Observations;
            task.Primary = dto.Primary;
            task.Status = (dto.StatusID == null || dto.StatusID == "")
                              ? null
                              : ManagerFactory.StatusManager.Get(new Guid(dto.StatusID));
            if (dto.CreateDate != null)
                task.CreateDate = TimeZone.CurrentTimeZone.ToLocalTime((DateTime) dto.CreateDate);
            if (dto.StartDate != null) task.StartDate = TimeZone.CurrentTimeZone.ToLocalTime((DateTime) dto.StartDate);
            if (dto.FinishDate != null)
                task.FinishDate = TimeZone.CurrentTimeZone.ToLocalTime((DateTime) dto.FinishDate);
            task.Target = dto.TargetDate;
            task.AssignedTo = (dto.ConsigneeID == null || dto.ConsigneeID == "")
                                 ? null
                                 : ManagerFactory.UserManager.GetUser(new Guid(dto.ConsigneeID));
            task.CreateTime = dto.CreateTime;
            task.Creator = dto.Creator;

            if (dto.Actuals != null)
            {
                //load tasks
                foreach (ActualsDTO actualDTO in dto.Actuals)
                {
                    task.AddActual(ActualsDTOMapper.MapFromDTO(actualDTO));
                }
            }

            return task;
        }
    }
}